<template>
  <div class="role">
    <div class="toolBar">
      <el-button
        size="small"
        type="primary"
        icon="el-icon-plus"
        @click="
          showAddRole = true;
          dialogTitle = '新增角色';
          addRole = {}
        "
        >新增角色</el-button
      >
      <el-button
        size="small"
        type="danger"
        icon="el-icon-delete"
        @click="deleteRole"
        >删除角色</el-button
      >
    </div>
    <div class="content">
      <el-table
        size="small"
        border
        :data="tableData"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55"> </el-table-column>
        <el-table-column prop="role" label="角色名称"></el-table-column>
        <el-table-column prop="roleName" label="角色描述"></el-table-column>
        <el-table-column align="center" width="100" label="操作">
          <template slot-scope="scope">
            <el-link type="primary" @click="edit(scope.row)">编辑</el-link>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <el-dialog :title="dialogTitle" :visible.sync="showAddRole">
      <el-form size="small">
        <el-form-item label="角色名称">
          <el-input
            v-model="addRole.role"
            placeholder="输入角色(英文字母)"
          ></el-input>
        </el-form-item>
        <el-form-item label="角色描述">
          <el-input
            v-model="addRole.roleName"
            placeholder="角色描述"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            @click="createRole"
            v-if="dialogTitle == '新增角色'"
            >确认添加</el-button
          >
          <el-button type="primary" @click="updateRole" v-else
            >确认修改</el-button
          >
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>
<script>
import * as api from "@/js/api";
export default {
  name: "Role",
  data() {
    return {
      tableData: [],
      showAddRole: false,
      addRole: {},
      selectedIds: [],
      dialogTitle: "新增角色",
    };
  },
  methods: {
    getRoleList() {
      api.getRoleList().then((res) => {
        this.tableData = res.data;
      });
    },
    createRole() {
      if (
        this.addRole.role == null ||
        this.addRole.roleName == null ||
        this.addRole.role.trim() == "" ||
        this.addRole.roleName.trim() == ""
      ) {
        this.$message({
          type: "warning",
          message: "请输入完整角色信息进行提交",
        });
        return;
      }
      api.addRole(this.addRole).then((res) => {
        this.$message({
          type: res.status ? "success" : "error",
          message: res.message,
        });
        this.showAddRole = false;
        this.getRoleList();
      });
    },
    handleSelectionChange(data) {
      this.selectedIds = [];
      data.forEach((element) => {
        this.selectedIds.push(element.id);
      });
    },
    deleteRole() {
      if (this.selectedIds.length != 0) {
        api.deleteRole(this.selectedIds).then((res) => {
          this.$message({
            type: res.status ? "success" : "error",
            message: res.message,
          });
          this.getRoleList();
        });
      }
    },
    edit(data) {
      this.dialogTitle = "编辑修改";
      this.addRole = data;
      this.showAddRole = true;
    },
    updateRole() {
      api.updateRole(this.addRole).then((res) => {
        this.$message({
          type: res.status ? "success" : "error",
          message: res.message,
        });
        this.getRoleList();
        this.showAddRole = false;
      });
    },
  },
  mounted() {
    this.getRoleList();
  },
};
</script>
<style scoped>
.role {
  padding: 10px;
}
.toolBar {
  text-align: left;
  border-bottom: 1px solid gray;
  padding-bottom: 5px;
}
</style>